package com.azzcs.mvc.rm;

import com.azzcs.mvc.servlet.HandlerExecutionChain;
import com.azzcs.mvc.servlet.HandlerInterceptor;
import com.azzcs.mvc.servlet.HandlerMapping;
import com.azzcs.mvc.servlet.HandlerMethod;
import com.azzcs.spring.anno.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: wzg
 * @Date: 2020/12/28 下午5:45
 */
@Component
public class RequestMappingHandlerMapping implements HandlerMapping {

    Map<String,HandlerMethod> handlerMethodMap;
    List<HandlerInterceptor> handlerInterceptors;

    public void setHandlerMethodMap(Map<String, HandlerMethod> handlerMethodMap) {
        this.handlerMethodMap = handlerMethodMap;
    }

    public void setHandlerInterceptors(List<HandlerInterceptor> handlerInterceptors) {
        this.handlerInterceptors = handlerInterceptors;
    }

    @Override
    public HandlerExecutionChain handle(HttpServletRequest req) {
        String requestURI = req.getRequestURI();
        HandlerMethod handlerMethod = handlerMethodMap.get(requestURI);
        return new HandlerExecutionChain(handlerMethod,handlerInterceptors);
    }
}
